﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace webnew.Common
{
    /// <summary>
    /// 数据类型转换
    /// </summary>
    public static class MyConvert
    {


        #region 一般转换
        /// <summary>
        /// int32
        /// </summary>
        public static int toInt32(object obj)
        {
            if (object.Equals(obj, DBNull.Value) || object.Equals(obj, null) || object.Equals(obj, string.Empty))
            {
                return 0;
            }
            else
            {
                return Convert.ToInt32(obj);
            }
        }

        public static Int16 toInt16(object obj)
        {
            if (object.Equals(obj, DBNull.Value) || object.Equals(obj, null) || object.Equals(obj, string.Empty))
            {
                return 0;
            }
            else
            {
                return Convert.ToInt16(obj);
            }
        }

        public static double toDouble(object obj)
        {
            if (object.Equals(obj, DBNull.Value) || object.Equals(obj, null) || object.Equals(obj, string.Empty))
            {
                return 0;
            }
            else
            {
                return Convert.ToDouble(obj);
            }
        }

        public static string toString(object obj)
        {
            if (object.Equals(obj, DBNull.Value) || object.Equals(obj, null))
            {
                return string.Empty;
            }
            else
            {
                return obj.ToString();
            }
        }

        public static Single toSingle(object obj)
        {
            if (object.Equals(obj, DBNull.Value) || object.Equals(obj, null) || object.Equals(obj, string.Empty))
            {
                return 0;
            }
            else
            {
                return Convert.ToSingle(obj);
            }
        }

        public static decimal todecimal(object obj)
        {
            if (object.Equals(obj, DBNull.Value) || object.Equals(obj, null) || object.Equals(obj, string.Empty))
            {
                return 0;
            }
            else
            {
                return Convert.ToDecimal(obj);
            }
        }

        public static DateTime toDateTime(object obj)
        {
            DateTime dtresult;
            try
            {

                DateTime.TryParse(obj.ToString(), out dtresult);
                return dtresult;
            }
            catch
            {
                return Convert.ToDateTime("2000-01-01 00:00:00");
            }
        }

        /// <summary>
        /// 判断是否为null,是null转换为DBNull.Value，否不转换
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        public static object toSqlNull(object obj)
        {
            if (object.Equals(obj, string.Empty) || object.Equals(obj.ToString(), "0001-1-1 0:00:00") || object.Equals(obj.ToString(), "0") || object.Equals(obj.ToString(), "0.00"))
                return System.DBNull.Value;
            return obj;
        }


        /// <summary>
        /// 检测是否为空时间，是转换空字符，否返回原值
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        public static String toStringByNullDateTime(object obj)
        {
            if (object.Equals(obj, DBNull.Value) || object.Equals(obj, null) || object.Equals(obj, string.Empty) || object.Equals(obj.ToString(), "0001-1-1 0:00:00"))
            {
                return "";
            }
            else
            {
                return obj.ToString();
            }
        }


        public static DateTime toMinDateTime(object obj)
        {
            DateTime dtresult;
            try
            {
                if (obj.ToString() != string.Empty)
                {
                    dtresult = Convert.ToDateTime(obj.ToString() + " 00:00:00");
                }
                else
                {
                    dtresult = Convert.ToDateTime("2000-01-01 00:00:00");
                }
                return dtresult;
            }
            catch
            {
                return DateTime.MinValue;
            }
        }
        public static DateTime toMaxDateTime(object obj)
        {
            DateTime dtresult;
            try
            {
                if (obj.ToString() != string.Empty)
                {
                    dtresult = Convert.ToDateTime(obj.ToString() + " 23:59:59");
                }
                else
                {
                    dtresult = Convert.ToDateTime("2100-01-01 23:59:59");
                }
                return dtresult;
            }
            catch
            {
                return DateTime.MaxValue;
            }
        }
        public static bool toBool(object obj)
        {
            if (object.Equals(obj, DBNull.Value) || object.Equals(obj, null) || object.Equals(obj, string.Empty))
            {
                return false;
            }
            else
            {
                return Convert.ToBoolean(obj);
            }
        }
        #endregion

        #region 可空型转换
        public static int? tonullint(object obj)
        {
            if (object.Equals(obj, DBNull.Value) || object.Equals(obj, null))
            {
                return null;
            }
            else
            {
                return toInt32(obj);
            }
        }

        public static Int16? tonullint16(object obj)
        {
            if (object.Equals(obj, DBNull.Value) || object.Equals(obj, null))
            {
                return null;
            }
            else
            {
                return (Int16?)obj;
            }
        }

        public static Double? tonulldouble(object obj)
        {
            if (object.Equals(obj, DBNull.Value) || object.Equals(obj, null))
            {
                return null;
            }
            else
            {
                return (Double?)obj;
            }
        }

        public static DateTime? tonulldatetime(object obj)
        {
            if (object.Equals(obj, DBNull.Value) || object.Equals(obj, null))
            {
                return null;
            }
            else
            {
                return (DateTime?)obj;
            }
        }
        #endregion
    }
}
